package com.jf.identity.config;

import com.jf.identity.dao.vis.ExamInfoDao;
import com.jf.identity.dao.vis.OrgCodeDao;
import com.jf.identity.datasource.DynamicDataSource;
import com.jf.identity.datasource.TargetDataSource;
import com.jf.identity.entity.ExamInfo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FCG
 */
@Configuration
public class AppConfig {
    @Resource
    private ExamInfoDao examInfoDao;
    @Resource
    private OrgCodeDao orgCodeDao;
    /**
     * 读取系统配置文件,machine.config
     *
     * @return machineConfig
     */
    @Bean
    public MachineConfig machineConfig() {
        MachineConfigLoader loader = new MachineConfigLoader();
        return loader.load();
    }

    @Bean
    public OrgCodeConfig orgCodeConfig(MachineConfig machineConfig) {
//        String currentOrgCode = "86.32";
        String currentOrgCode = machineConfig.getCurrentOrgCode();
//        String currentOrgIdenCode = "ABC123";
        String currentOrgIdenCode = machineConfig.getCurrentOrgIdenCode();
        return new OrgCodeConfig(currentOrgCode, currentOrgIdenCode);
    }

    @Bean
    public ExamConfig examConfig(TargetDataSource targetDataSource,
                                 DynamicDataSource dynamicDataSource,
                                 MachineConfig machineConfig) {
//        String provinceCode = orgCodeDao.getProvinceOrgCode();
//        List<ExamInfo> list = examInfoDao.getExamInfos(provinceCode);
        List<ExamInfo> list = examInfoDao.getAllExamIds();
        ExamConfig examConfig = new ExamConfig(list);
        Map<String, DataSource> targetDataSources = targetDataSource.getTargetDataSource(machineConfig,examConfig);
        Map<Object, Object> dataSources = new HashMap<>(targetDataSources.size());
        targetDataSources.forEach(dataSources::put);
        dynamicDataSource.setTargetDataSources(dataSources);
        dynamicDataSource.resolveDataSource();
        return examConfig;
    }


}




















